package com.lengyu.vxminiprogram.iot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lengyu.vxminiprogram.common.HttpRequestUtil;
import com.lengyu.vxminiprogram.common.HttpResultDto;
import com.lengyu.vxminiprogram.controller.WorkOrderController;
import com.lengyu.vxminiprogram.redis.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 额外调用第三封平台接口
 */
@Service
public class LengyuIotUtil {

    private final static Logger logger = LoggerFactory.getLogger(WorkOrderController.class);
    @Autowired
    RedisUtils redisUtils;

    public String getToken(){
        String token = redisUtils.get("lengyu_iot_token");
        if(StringUtils.isNotBlank(token)){
            return token;
        }
       String result =  HttpRequestUtil.doGet(LengyuIotConst.BASE_URL+"/oauth/login?username="+
               LengyuIotConst.username+"&password="+LengyuIotConst.password,
               "");

       logger.info(result);
       HttpResultDto resultObj = JSONObject.parseObject(result,HttpResultDto.class);
       if(resultObj.getCode() == 20000){
           JSONObject data = JSONObject.parseObject(resultObj.getData().toString());
           redisUtils.set("lengyu_iot_token",data.getString("token"),2, TimeUnit.HOURS);
           token = data.getString("token");
           logger.info("token"+ data.getString("token"));
       }
       return token;
    }

    public JSONArray getTagsArray(JSONObject params){
        String token = getToken();
        if(StringUtils.isBlank(token)){
            return null;
        }
        String result =  HttpRequestUtil.doPost(LengyuIotConst.BASE_URL+"/device-tag/jsonData",token,params.toJSONString());

        HttpResultDto resultObj = JSONObject.parseObject(result,HttpResultDto.class);
        if(resultObj.getCode() == 20000) {
            JSONArray data = JSONObject.parseArray(resultObj.getData().toString());

            return data;
        }
        else{
            return null;
        }
    }

    public JSONArray getTagHistoryDataForEchartCompare(JSONObject params){
        String token = getToken();
        if(StringUtils.isBlank(token)){
            return null;
        }
        String result =  HttpRequestUtil.doPost(LengyuIotConst.BASE_URL+"/device-property/getTagHistoryDataForEchartCompare",token,params.toJSONString());

        HttpResultDto resultObj = JSONObject.parseObject(result,HttpResultDto.class);
        if(resultObj.getCode() == 20000) {
            JSONArray data = JSONObject.parseArray(resultObj.getData().toString());
            return data;
        }
        else{
            return null;
        }
    }

    public JSONObject getTagHistoryDataForEchart(JSONObject params){
        String token = getToken();
        if(StringUtils.isBlank(token)){
            return null;
        }
        String result =  HttpRequestUtil.doPost(LengyuIotConst.BASE_URL+"/device-property/getTagHistoryDataForEchart",token,params.toJSONString());
        logger.info(result);
        HttpResultDto resultObj = JSONObject.parseObject(result,HttpResultDto.class);
        if(resultObj.getCode() == 20000) {
            JSONObject data = JSONObject.parseObject(resultObj.getData().toString());
            return data;
        }
        else{
            return null;
        }
    }
}
